package it.unibz.ti2.internetPizzeria.dao;

import it.unibz.ti2.internetPizzeria.entities.Drink;
import it.unibz.ti2.internetPizzeria.entities.Pizza;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;

/**
 * Class responsible for searching pizzas and drinks in database
 */
public class ProductDAO extends BaseDAO {

	/**
	 * Retrieves pizzas of specified availability from database
	 * @param available pizzas' availability
	 * @return list of pizzas of specified availability
	 */
	public List<Pizza> getPizzas(boolean available) {
		Criteria criteria = session.createCriteria(Pizza.class);
		criteria.add(Expression.eq("available", available));
		List<Pizza> list = (List<Pizza>) criteria.list();
		return list;
	}

	/**
	 * Retrieves pizza of specified id from database
	 * @param id pizza's id
	 * @return pizza of specified id
	 */
	public Pizza getPizza(long id) {
		Criteria criteria = session.createCriteria(Pizza.class);
		criteria.add(Expression.idEq(id));
		Pizza result = (Pizza) criteria.uniqueResult();
		return result;
	}

	/**
	 * Retrieves drinks of specified availability from database
	 * @param available drinks' availability
	 * @return list of drinks of specified availability
	 */
	public List<Drink> getDrinks(boolean available) {
		Criteria criteria = session.createCriteria(Drink.class);
		criteria.add(Expression.eq("available", available));
		List<Drink> list = (List<Drink>) criteria.list();
		return list;
	}

	/**
	 * Retrieves drink of specified id from database
	 * @param id drink's id
	 * @return drink of specified id
	 */
	public Drink getDrink(long id) {
		Criteria criteria = session.createCriteria(Drink.class);
		criteria.add(Expression.idEq(id));
		Drink result = (Drink) criteria.uniqueResult();
		return result;
	}

}